﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using ShaderProject;
using Chromos.Actors;

namespace Chromos
{
	class Collision
	{
		public List<Sprite.Axis> Obs;
		public Player player;
		public bool noFloor, currentJump, moveLeft, moveRight;
		public int lastDirection;
		public float lastX, lastY;
		public Controller con;

		public bool Collide(int place)
		{
			int i, sizeOf, playerWidth = 28, playerHeight = 30;
			bool foundCollide = false;
			bool collide = false;
			sizeOf = Obs.Count();
			int tempX, tempY, tempHeight, tempWidth;
			for (i = 0; (i < sizeOf) && (!foundCollide); i++)
			{
				tempX = Obs.ElementAt(i).returnX();
				tempY = Obs.ElementAt(i).returnY();
				tempHeight = Obs.ElementAt(i).returnHeight();
				tempWidth = Obs.ElementAt(i).returnWidth();
				Rectangle tempRect = new Rectangle(tempX, tempY, tempWidth, tempHeight);
				Rectangle playerRect = new Rectangle((int)player.position.X + 1, (int)player.position.Y, playerWidth, playerHeight);
				//This is to test for intersection
				if (place == 0)
				{
					if (tempRect.Intersects(playerRect))
					{
						//On top of brick
						if ((tempRect.Top < playerRect.Bottom) && (tempRect.Top > playerRect.Top))
						{
							noFloor = false;
						}
						//Under brick jumping up
						if ((tempRect.Bottom > playerRect.Top) && (tempRect.Bottom < playerRect.Bottom) && (currentJump == true))
						{
							//lastY = tempRect.Bottom;
							currentJump = false;
							noFloor = true;

						}
						//falling into a wall while pressing left (used to cause player to stick to wall)
						else if ((con.isActionPressed("LLeft")) && (tempRect.Right > playerRect.Left) && (tempRect.Right < playerRect.Right))
						{
							//lastX = tempRect.Right + 1;
							lastDirection = 0;
							//player.position.X++;
							moveLeft = false;
						}
						//falling into a wall while pressing right (used to cause player to stick to wall)
						else if (((con.isActionPressed("LRight")) && (tempRect.Left < playerRect.Right) && (tempRect.Left > playerRect.Left)))
						{
							//lastX = tempRect.Left - (tempWidth + 1);
							lastDirection = 0;
							//player.position.X--;
							moveRight = false;
						}
						player.position.X = lastX;
						player.position.Y = lastY;
						foundCollide = true;
						collide = true;
					}
				}
				//This is to test if the player should be falling or not
				if (place == 1)
				{
					playerRect = new Rectangle((int)player.position.X + 1, (int)player.position.Y + 1, playerWidth, playerHeight);
					if (tempRect.Intersects(playerRect))
					{
						collide = true;
						foundCollide = true;
					}
				}
				if (place == 2)
				{
					//Wall on Right
					playerRect = new Rectangle((int)player.position.X + 1 + 3, (int)player.position.Y, playerWidth, playerHeight);
					if (tempRect.Intersects(playerRect))
					{
						collide = true;
						foundCollide = true;
					}
				}
				if (place == 3)
				{
					//Wall on Left
					playerRect = new Rectangle((int)player.position.X + 1 - 3, (int)player.position.Y, playerWidth, playerHeight);
					if (tempRect.Intersects(playerRect))
					{
						collide = true;
						foundCollide = true;
					}
				}
			}
			return collide;
		}

		public bool CollideWithPlayer(Enemy enemy)
		{
			Rectangle playerRect = new Rectangle((int)player.position.X, (int)player.position.Y, player.getCurrentImage().Height, player.getCurrentImage().Width);
			Rectangle objectRect = new Rectangle((int)enemy.position.X, (int)enemy.position.Y, enemy.getCurrentImage().Height, enemy.getCurrentImage().Width);
			return (playerRect.Intersects(objectRect));
		}

		public bool CollideWithEnemy(Enemy enemy, Sprite.Axis shots)
		{
			Rectangle playerRect = new Rectangle(shots.returnX(), shots.returnY(), shots.returnHeight(), shots.returnWidth());
			Rectangle objectRect = new Rectangle((int)enemy.position.X, (int)enemy.position.Y, enemy.getCurrentImage().Height, enemy.getCurrentImage().Width);
			return (playerRect.Intersects(objectRect));
		}

		public bool CollideWithBrickShot(Sprite.Axis shots)
		{
			int i, sizeOf;
			//noFloor = true;
			bool foundCollide = false;
			bool collide = false;
			sizeOf = Obs.Count();
			int tempX, tempY, tempHeight, tempWidth;
			for (i = 0; (i < sizeOf) && (!foundCollide); i++)
			{
				tempX = Obs.ElementAt(i).returnX();
				tempY = Obs.ElementAt(i).returnY();
				tempHeight = Obs.ElementAt(i).returnHeight();
				tempWidth = Obs.ElementAt(i).returnWidth();
				Rectangle tempRect = new Rectangle(tempX - 1, tempY - 1, tempHeight, tempWidth);
				Rectangle objectRect = new Rectangle(shots.returnX(), shots.returnY(), shots.returnHeight(), shots.returnWidth());
				//This is to test for intersection
				if (tempRect.Intersects(objectRect))
				{
					//On top of brick
					if ((tempRect.Top < objectRect.Bottom) && (tempRect.Top > objectRect.Top))
					{
						noFloor = false;
					}
					//Under brick jumping up
					if ((tempRect.Bottom > objectRect.Top) && (tempRect.Bottom < objectRect.Bottom) && (currentJump == true))
					{
						currentJump = false;
						noFloor = true;
					}
					//Needs to look for collide here if speed > 1...
					//The problem is, that when the speed is > 1 the character is not placed in an optimal space after //collision
					//playerStartingPosition.X = lastX;
					//pos1.Y = lastY;
					foundCollide = true;
					collide = true;
				}
				//This is to test if the player should be falling or not
				//The problem here is that the player will continue to fall and bounce back up after a //collision is detected, causing stuttering and slowing the game
				/*objectRect = new Rectangle((int)pos1.X, (int)pos1.Y + 1, 30, 30);
				if (tempRect.Intersects(objectRect))
				{
					collide = true;
					foundCollide = true;
				}*/
			}
			return collide;
		}

		public bool CollideWithBrick(Vector2 position)
		{
			int i, sizeOf;
			//noFloor = true;
			bool foundCollide = false;
			bool collide = false;
			sizeOf = Obs.Count();
			int tempX, tempY, tempHeight, tempWidth;
			for (i = 0; (i < sizeOf) && (!foundCollide); i++)
			{
				tempX = Obs.ElementAt(i).returnX();
				tempY = Obs.ElementAt(i).returnY();
				tempHeight = Obs.ElementAt(i).returnHeight();
				tempWidth = Obs.ElementAt(i).returnWidth();
				Rectangle tempRect = new Rectangle(tempX - 1, tempY - 1, tempHeight, tempWidth);
				Rectangle objectRect = new Rectangle((int)position.X, (int)position.Y, 30, 29);
				//This is to test for intersection
				if (tempRect.Intersects(objectRect))
				{
					//On top of brick
					if ((tempRect.Top < objectRect.Bottom) && (tempRect.Top > objectRect.Top))
					{
						noFloor = false;
					}
					//Under brick jumping up
					if ((tempRect.Bottom > objectRect.Top) && (tempRect.Bottom < objectRect.Bottom) && (currentJump == true))
					{
						currentJump = false;
						noFloor = true;
					}
					//Needs to look for collide here if speed > 1...
					//The problem is, that when the speed is > 1 the character is not placed in an optimal space after //collision
					//playerStartingPosition.X = lastX;
					//pos1.Y = lastY;
					foundCollide = true;
					collide = true;
				}
				//This is to test if the player should be falling or not
				//The problem here is that the player will continue to fall and bounce back up after a //collision is detected, causing stuttering and slowing the game
				/*objectRect = new Rectangle((int)pos1.X, (int)pos1.Y + 1, 30, 30);
				if (tempRect.Intersects(objectRect))
				{
					collide = true;
					foundCollide = true;
				}*/
			}
			return collide;
		}
	}
}
